<?php

/**
 * 自动生成 MerWxMenuController
 * 
 * Apps/Backend/Controller/MerWxMenuController.class.php
 * Zix @ 2015-06-16
 */

namespace Backend\Controller;

use Common\Service\MerchantService ;
use Common\Service\MerWxMenuService;
use Common\Service\MerWxSettingService;
use Common\Service\WechatSdk;

class MerWxMenuController extends BackendController {

  public function index( $mer_id , $wx_id ) {

    $data = $this->_init_data();
    $data['module_name'] = '微信菜单';
    $data['module_url'] = U( $this->base_path . 'index');

    $data['catalog_name'] = '商户';
    $data['catalog_url'] = U( $this->base_path . 'index') .'/' . $mer_id;
    
    $Merchant = MerchantService::instance() ;
    $data['merchant_data'] = $Merchant->get_by_id( $mer_id );
    if ( empty( $data['merchant_data'] ) ) {
      gameover('商户不存在');
    }
    
    $MerWxMenu = MerWxMenuService::instance();

    $data['config'] = array(
      'mer_id' => $mer_id ,
      'wx_id' => $wx_id ,
      'module_url' => U($this->base_path . 'index'),
      'wx_menu'   => json_decode( $MerWxMenu->get_by_id( $wx_id ) , TRUE) ,
      'save_menu_url' => U($this->base_path . 'save_menu'),
      'get_menu_url' => U($this->base_path . 'get_menu'),
      'post_menu_url' => U($this->base_path . 'post_menu'),
    );

    $data['param'] = array();

    $this->display_with_layout($data, $this->controller .':index');
  }

  function add_main_menu() {
    $this->display('main_menu');
  }
  
  function get_menu() {
    $wx_id = I('get.wx_id');
    
    $MerWxSetting = MerWxSettingService::instance() ;
    $wx_data = $MerWxSetting->get_by_id($wx_id);
    if ( empty( $wx_data['app_id'] ) || empty($wx_data['app_secret']) ) {
      $this->ajaxReturn(ajax_arr('APPID或APPSECRET未设置'));
    }
    
    $WechatSdk = new WechatSdk( $wx_data['app_id'] , $wx_data['app_secret'] );
    $ret = $WechatSdk->get_menu();
    if ( !$ret['status'] ) {
      $this->ajaxReturn($ret);
    }
    
    //开始解析菜单
    $MerWxMenu = MerWxMenuService::instance();
    $data = $MerWxMenu->wx_data_2_self_data($ret);
    
    unset($ret['menu']);
    $ret['data'] = $data ;
    $MerWxMenu->save_menu($wx_id, $data);
    
    $this->ajaxReturn($ret);
  }
  
  function save_menu() {
    $data = I('post.data');
    $wx_id = I('post.wx_id');

    foreach ( $data['button'] as $key => $row ) {
      if ( !isset( $row['sub_button'] ) ) {
        $data['button'][$key]['sub_button'] = array();
      }
    }
    
    $MerWxMenu = MerWxMenuService::instance();
    $ret  = $MerWxMenu->save_menu($wx_id, $data);
    
    $this->ajaxReturn($ret);
  }
    
  function post_menu($id = 0) {
    $data = I('post.data');
    $wx_id = I('post.wx_id');
    
    foreach ( $data['button'] as $key => $row ) {
      if ( !isset( $row['sub_button'] ) ) {
        $data['button'][$key]['sub_button'] = array();
      }
    }
    
    //先保存
    $MerWxMenu = MerWxMenuService::instance();
    $MerWxMenu->save_menu($wx_id, $data);
    
    //转换为微信格式
    $wx_data = $MerWxMenu->self_data_2_wx_data($data);
    
    //取APPID和APPSECRET
    $MerWxSetting = MerWxSettingService::instance() ;
    $setting_data = $MerWxSetting->get_by_id($wx_id);
    if ( empty( $setting_data['app_id'] ) || empty($setting_data['app_secret']) ) {
      $this->ajaxReturn(ajax_arr('APPID或APPSECRET未设置'));
    }
    
    //开始发布
    $WechatSdk = new WechatSdk( $setting_data['app_id'] , $setting_data['app_secret'] );
    $ret = $WechatSdk->post_menu($wx_data);
   
    $this->ajaxReturn($ret);
  }

}
